package com.app.backend.config;

import com.app.backend.Interceptor.UserInterceptor;
import com.app.backend.common.annotation.RequestJsonHandlerMethodArgumentResolver;
import com.app.backend.config.apiVersion.CustomRequestMappingHandlerMapping;
import com.app.backend.config.fastJson.BaseFastJsonConfig;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.List;
import java.util.Properties;


/**
 * @program: WebConfiguration
 * @description: web配置
 * @author: Zhun.Xiao
 * @create: 2018-11-22 13:12
 **/
@Slf4j
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport implements WebMvcRegistrations {


    /**
     * 添加 RequestJson 注释方法，支持post直接获取String参数
     * 只适用于获取单个参数
     * @param argumentResolvers
     */
    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new RequestJsonHandlerMethodArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);
        return handlerMapping;
    }

    //配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper() {
        log.info("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        /**
         * 配置mysql数据库的方言
         */
        properties.setProperty("dialect", "mysql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }

    @Bean
    public UserInterceptor userInterceptor() {
        return new UserInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userInterceptor()).addPathPatterns("/v1/sys/**")
                .addPathPatterns("/**/export")
                .excludePathPatterns("/v1/sys/login/key/**")
                .excludePathPatterns("/v1/sys/login/login/**")
                .excludePathPatterns("/v1/sys/login/sso/login/**")
                .excludePathPatterns("/v1/sys/login/logout/**")
                .excludePathPatterns("/**/download")
                .excludePathPatterns("/**/attachment")
                .excludePathPatterns("/**/upload")
                .excludePathPatterns("/api/v1/act/**");
    }

    /**
     * 添加fastJsonHttpMessageConverter到converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("configureMessageConverters........");
       /* for (int i = converters.size() - 1; i >= 0; i--) {
            if (converters.get(i) instanceof MappingJackson2CborHttpMessageConverter) {
                converters.remove(i);
            }
        }*/
        converters.add(BaseFastJsonConfig.stringConverter());
        converters.add(BaseFastJsonConfig.fastJsonHttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}
